package com.wlstock.funi.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.wlstock.funi.R;
import com.wlstock.funi.constant.URLConstant;
import com.wlstock.funi.model.OperateModel;
import com.wlstock.funi.model.OperateTrackSetModel;
import com.wlstock.funi.request.CustomStringRequest;
import com.wlstock.funi.request.EventBusConstants;
import com.wlstock.funi.request.NetWorkListener;
import com.wlstock.funi.utils.CaculateUtils;
import com.wlstock.funi.utils.StrUtils;
import com.wlstock.funi.utils.TimeUtils;
import com.wlstock.funi.utils.TradeSetUtils;
import com.wlstock.funi.widget.SpecificSetItemView;

public class SpecificSetActivity extends FundBaseActivity implements OnClickListener, NetWorkListener{
	
	private LinearLayout inflateView;
	private ArrayList<OperateTrackSetModel> setModels;
	
	private EditText ed_b_offset,ed_b_money,ed_s_offset;
	private CheckBox cb_b_offeset,cb_s_offeset;
	private Button  btn_global_submit;
	private Button btn_submit;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_specific_set);
		inflateView = (LinearLayout) findViewById(R.id.layout_specific_contain);
		btn_submit = (Button) findViewById(R.id.btn_submit);
		btn_submit.setOnClickListener(this);
		btn_global_submit = (Button) findViewById(R.id.btn_global_submit);
		btn_global_submit.setOnClickListener(this);
		ed_b_offset = (EditText)findViewById(R.id.ed_b_global_offset);
		cb_b_offeset = (CheckBox)findViewById(R.id.cb_b_global_offset);
		ed_b_money  = (EditText)findViewById(R.id.ed_b_global_money);
		ed_s_offset  = (EditText)findViewById(R.id.ed_s_global_offset);
		cb_s_offeset  = (CheckBox)findViewById(R.id.cb_s_global_offset);
		
		
		HashMap<String, String> parms = new HashMap<String, String>();
		parms.put("act", "1");
		
		CustomStringRequest curRequest = new CustomStringRequest();
		curRequest.networkResponseListener = this;
		curRequest.requestGet(URLConstant.WLHOST, URLConstant.WL_TRADE_TRACK,null,parms);
		
	}
	

	@Override
	public void onClick(View v) {
		// TODO 保存操作
		switch (v.getId()) {
		case R.id.btn_global_submit://全局预设置
			if (setModels!=null && setModels.size()>0 && inflateView.getChildCount()>0) {
				for(int i = 0 ; i < inflateView.getChildCount(); i++){
					SpecificSetItemView view = (SpecificSetItemView) inflateView.getChildAt(i);
					OperateTrackSetModel mode = view.getMode();
					if (!StrUtils.isNullOrEmpty(ed_b_money.getText().toString())) {
						mode.buyMoney = getFloatVal(ed_b_money.getText());	
					}
					if (!StrUtils.isNullOrEmpty(ed_b_offset.getText().toString())) {
						mode.buyOffset = getFloatVal(ed_b_offset.getText());	
					}
					if (!StrUtils.isNullOrEmpty(ed_s_offset.getText().toString())) {
						mode.sailOffset= getFloatVal(ed_s_offset.getText());	
					}
					mode.isTrackBuy = cb_b_offeset.isChecked();
					mode.isTrackSaile = cb_s_offeset.isChecked();
					view.setMode(mode);
					view.updateSetItem(mode);
				}
			}
			break;
		case R.id.btn_submit:
			for(int i = 0 ; i < inflateView.getChildCount(); i++){
				SpecificSetItemView view = (SpecificSetItemView) inflateView.getChildAt(i);
				OperateTrackSetModel mode = view.getMode();
				TradeSetUtils.saveSpecificMode(this, mode);
			}
			this.finish();
			break;
		default:
			break;
		}
	}

	
	private float getFloatVal(Editable text) {
		float f = .0f;
		try {
			f = Float.valueOf(text.toString());
		} catch (Exception e) {
		}
		return f;
	}

	@Override
	public void onErrorResponse(VolleyError error, EventBusConstants eventCode) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onErrorResponse(VolleyError error, String eventCode) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResponse(String response, EventBusConstants eventCode) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResponse(String response, String eventCode) {
		// TODO Auto-generated method stub
		if (URLConstant.WL_TRADE_TRACK.equals(eventCode)) {
			try {
				JSONObject obj = new JSONObject(response);
				Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
				String format = TimeUtils.patternDate(obj.getString("datas"));
				List<OperateModel> operList = gson.fromJson(format, new TypeToken< List<OperateModel> >(){}.getType());
				operList = CaculateUtils.filterRepeatModels(operList);
				setModels = new ArrayList<OperateTrackSetModel>();
				for (int i = 0; i < operList.size(); i++) {
					OperateTrackSetModel model = new OperateTrackSetModel();
					OperateModel operateModel = operList.get(i);
					model.setFundid(operateModel.getFundid());
					model.setFundName(operateModel.getFundName());
					model.setProfitRate(operateModel.getProfitRate());
					model.buyMoney = TradeSetUtils.getTradeMoney(this, operateModel.getFundid());
					model.buyOffset = TradeSetUtils.getTradeOffset(this, operateModel.getFundid(), TradeSetUtils.TRADE_TYPE_BUY_OFFSET);
					model.sailOffset = TradeSetUtils.getTradeOffset(this, operateModel.getFundid(), TradeSetUtils.TRADE_TYPE_SAILE_OFFSET);
					model.isTrackBuy = TradeSetUtils.getIsTrack(this, operateModel.getFundid(), TradeSetUtils.TRADE_IS_BUY_TARCK);
					model.isTrackSaile = TradeSetUtils.getIsTrack(this, operateModel.getFundid(), TradeSetUtils.TRADE_IS_SAILE_TARCK);
					
					setModels.add(model);
					SpecificSetItemView spView = new SpecificSetItemView(this, null, model);
					inflateView.addView(spView);
				}
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
